﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Motoball.screenmanager;
using Motoball.Paddles;


namespace Motoball.screens
{
    public class WinningScreen:SplashScreen
    {
        Vector3 winnerColor;
        BasicModel winnerModel1;
        BasicModel winnerModel2;
        BasicModel curWinnerModel;

        float rotation = 0;
        ModelBone[] rings;
        Matrix[] ringMatrices;
        TimeSpan timer;
       
        public WinningScreen(string screenName)
            : base(screenName)
        {
        }

        #region Methods
        public override void LoadContent()
        {
            ContentManager content = screenManager.Content;
            //winnerModel1 = new BasicModel(content.Load<Model>("Models\\WinnerModel_1"));
            //winnerModel2 = new BasicModel(content.Load<Model>("Models\\WinnerModel_2"));

            winnerModel1 = new BasicModel(content.Load<Model>("Models\\Winning_Sreen_Player_1"));
            winnerModel2 = new BasicModel(content.Load<Model>("Models\\Winning_Sreen_Player_2"));
            winnerModel1.World *= Matrix.CreateRotationZ(MathHelper.Pi);
            winnerModel1.World *= Matrix.CreateRotationX(MathHelper.Pi);
            winnerModel1.World *= Matrix.CreateTranslation(0, Camera.gameplayCamDistance / 1.15f, 50);
            winnerModel2.World *= Matrix.CreateRotationZ(MathHelper.Pi);
            winnerModel2.World *= Matrix.CreateRotationX(MathHelper.Pi);
            winnerModel2.World *= Matrix.CreateTranslation(0, Camera.gameplayCamDistance / 1.15f, 50);
            curWinnerModel = winnerModel1;

            rings = new ModelBone[3];
            ringMatrices = new Matrix[3];

            List<string> keys = new List<string>(winnerModel1.diffColors.Keys);
            for (int i = 0; i < keys.Count; i++)
            {
                winnerModel1.diffColors[keys[i]] = new Vector3(0f);
            }
        }
        
        public override void Draw(GameTime gameTime)
        {
            
            foreach (ModelMesh mesh in curWinnerModel.model.Meshes)
            {
                foreach (BasicEffect be in mesh.Effects)
                {
                    be.SpecularColor = curWinnerModel.diffColors[mesh.Name];
                }
            }
            rotation += (float)gameTime.ElapsedGameTime.TotalSeconds;
            if (rotation > MathHelper.TwoPi)
                rotation -= MathHelper.TwoPi;
            Matrix outerRing = Matrix.CreateRotationX(rotation);
            Matrix centerRing = outerRing * Matrix.CreateRotationY(rotation);
            Matrix innerRing = centerRing * Matrix.CreateRotationZ(rotation);

            rings[0].Transform = outerRing * ringMatrices[0];
            rings[1].Transform = centerRing * ringMatrices[1];
            rings[2].Transform = innerRing * ringMatrices[2];
            base.Draw(gameTime);
        
            curWinnerModel.Draw(Camera);
        }

        public void Enter(Paddle winner)
        {
            AudioManager.PlayMusic("victory");
            if (winner.PlayerIndex == 0)
                curWinnerModel = winnerModel1;

            else
                curWinnerModel = winnerModel2;

            rings[0] = curWinnerModel.model.Bones["Ring1"];
            ringMatrices[0] = rings[0].Transform;
            rings[1] = curWinnerModel.model.Bones["Ring2"];
            ringMatrices[1] = rings[1].Transform;
            rings[2] = curWinnerModel.model.Bones["Ring3"];
            ringMatrices[2] = rings[2].Transform;

            //ContentStorage.baseIntensity = ContentStorage.bloomIntensity = 5f;
            //curWinnerModel.bloomingMeshes["Kugel"] = true;
            curWinnerModel.diffColors["Kugel"] = winner.Col3;
            
        }

        public override void Update(GameTime gameTime)
        {
            timer += gameTime.ElapsedGameTime;

            if (screenManager.Input.MenuCancel || screenManager.Input.MenuSelect || timer.TotalSeconds >= 20)
            {
                AudioManager.PlayMusic("");
                CameraMovementScreen camMovement = (CameraMovementScreen)screenManager.GetScreen("cameraMovementScreen");
                camMovement.StartCameraMovement(Camera.gameplayView, Camera.menuView);
                screenManager.AddScreen(camMovement);
                GameplayScreen gameplay = (GameplayScreen)screenManager.GetScreen("gameplayScreen");
                screenManager.RemoveScreen(gameplay);
                Exit();
            }
        }
        public override void Reset()
        {
            base.Reset();
            rotation = 0;
            timer = TimeSpan.Zero;
        }

        
        #endregion

        #region Properties
        private Vector3 WinnerColor
        {
            get { return winnerColor; }
            set { winnerColor = value; }
        }
        #endregion
    }
}
